// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import SdkManager from "../manager/SdkManager";
import DataManager from "../manager/DataManager";
import ToastManager from "../manager/ToastManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class OverLayer extends BaseLayer {

    panel: cc.Node = null
    btnClose: cc.Node = null
    btnRestart: cc.Node = null
    btnShare: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnClose = cc.find('btn_close', this.panel)
        this.btnRestart = cc.find('buttons/btn_restart', this.panel)
        this.btnShare = cc.find('buttons/btn_share', this.panel)
        this.btnClose.on('click', this.onCloseClick, this)
        this.btnRestart.on('click', this.onRestartClick, this)
        this.btnShare.on('click', this.onShareClick, this)
    }

    onDestroy() {
        this.btnClose.off('click', this.onCloseClick, this)
        this.btnRestart.off('click', this.onRestartClick, this)
        this.btnShare.off('click', this.onShareClick, this)
    }

    onEnable() {
        this.zoomIn(this.panel)
        SdkManager.instance.showInterstitialAd()
        this.btnRestart.getChildByName('ad').active = DataManager.instance.power <= 0
    }

    onDisable() { }

    onRestartClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (this.btnRestart.getChildByName('ad').active) {
            SdkManager.instance.showVideoAd((msg: string) => {
                if (!SdkManager.instance.getPlatform()) {
                    ToastManager.instance.show(msg, { gravity: 'TOP', bg_color: cc.color(102, 202, 28, 255) })
                }
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN_PK, false)
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.OVER, false)
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY)
            }, (msg: string) => {
                ToastManager.instance.show(msg, { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
            })
        } else {
            DataManager.instance.power -= 1
            DataManager.instance.save()
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN_PK, false)
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.OVER, false)
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY)
        }
    }

    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataManager.instance.status = ENUM_GAME_STATUS.UNRUNING
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN_PK, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.OVER, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
    }

    setLabel(isWin: boolean) {
        const title = cc.find('style/panel/title', this.node)
        title.getComponent(cc.Label).string = isWin ? '胜利' : '失败'
        const win = cc.find('style/panel/tip/win', this.node)
        const lose = cc.find('style/panel/tip/lose', this.node)
        win.getComponent(cc.Label).string = `胜场:${DataManager.instance.countWin}`
        lose.getComponent(cc.Label).string = `负场:${DataManager.instance.countLose}`
    }

    onShareClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (SdkManager.instance.getPlatform()) {
            SdkManager.instance.activeShare()
        } else {
            ToastManager.instance.show('仅支持小游戏平台', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
        }
    }
}
